package com.vooda.business_admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vooda.business_admin.common.ReqDataTableDto;
import com.vooda.business_admin.common.RspDataTableDto;
import com.vooda.business_admin.dao.MarketMapper;
import com.vooda.business_admin.entity.Market;
import com.vooda.business_admin.service.MarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional(readOnly = true)
public class MarketServiceImpl implements MarketService{

    @Autowired
    private MarketMapper marketMapper;

    @Override
    @Transactional(readOnly = false)
    public String deleteByPrimaryKey(Long id) {
        Market market = new Market();
        market.setId(id);
        market.setIsDel(true);
        return updateByPrimaryKeySelective(market);
    }

    @Override
    @Transactional(readOnly = false)
    public String insertSelective(Market record) {
        Long maxId = marketMapper.findMaxId();
        record.setId(maxId+1);
        record.setCreateTime(new Date());
        record.setIsDel(false);
        record.setModifiedTime(new Date());
        return marketMapper.insertSelective(record)>0?"success":"fail";
    }

    @Override
    public Market selectByPrimaryKey(Long id) {
        return marketMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false)
    public String updateByPrimaryKeySelective(Market record) {
        record.setModifiedTime(new Date());
        return marketMapper.updateByPrimaryKeySelective(record)>0?"success":"fail";
    }

    @Override
    public RspDataTableDto<Market> findAllMarketList(ReqDataTableDto dataTableDTO, String market) {

        PageHelper.startPage(dataTableDTO.getPage()+1,dataTableDTO.getLength());
        List<Market> list = marketMapper.findAllMarketList(market);
        PageInfo<Market> pageInfo =  new PageInfo<Market>(list);
        return new RspDataTableDto<Market>(dataTableDTO.getDraw(),pageInfo.getTotal(),pageInfo.getList());

    }

    @Override
    @Transactional(readOnly = false)
    public String saveMarket(Market market) {
        return market.getId() != null?updateByPrimaryKeySelective(market):insertSelective(market);
    }

    @Override
    public List<Market> getMarketList() {
        return marketMapper.findAllMarketList(null);
    }
}
